local skynet = require "skynet"
local base = require "base"
local timer = require "timer"
local filelog = require "filelog"
local qhbtool = require "qhbtool"

require "hbslenum"

local server = nil
local tabletimer = {}

function tabletimer.init(service)
	if server == nil then server = service end
end

function tabletimer.timer(msgname, timerid, noticemsg)
	local f = tabletimer[msgname]
	if f and type(f) == "function" then
		f(timerid, noticemsg)
	end
end

function tabletimer.delete_table(timerid, noticemsg)
    local table_data = server.table_data
    if noticemsg.table_index ~= table_data.conf.table_index then
        return
    end
    if timerid ~= table_data.delete_table_timer_id then
        return 
    end
    table_data.delete_table_timer_id = -1
    server.CMD["delete"](noticemsg.reason)
end

function tabletimer.wait_player_burylandmine(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.timer_id = -1
end

function tabletimer.one_game_start_over(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	local tablelogic = server:get_logicbyname("tablelogic")
	table_data.timer_id = -1
	if table_data.state == HBSLTableState.TABLE_STATE_ONE_GAME_START_OVER then
		table_data.state = HBSLTableState.TABLE_STATE_PLAYER_QHB
		local gamelogic = tablelogic.get_gamelogic()
		gamelogic.run(table_data.gamelogic)
	end
end

function tabletimer.player_qhb_outoftime(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.timer_id = -1
	local tablelogic = server:get_logicbyname("tablelogic")
	if table_data.state == HBSLTableState.TABLE_STATE_WAIT_PLAYER_QHB then
		table_data.state = HBSLTableState.TABLE_STATE_ONE_GAME_END
		local gamelogic = tablelogic.get_gamelogic()
		gamelogic.run(table_data.gamelogic)
	end
end

function tabletimer.one_game_end_over(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.timer_id = -1
	local tablelogic = server:get_logicbyname("tablelogic")
	if table_data.state == HBSLTableState.TABLE_STATE_ONE_GAME_END_OVER then
		table_data.state = HBSLTableState.TABLE_STATE_ONE_GAME_END_AFTER
		local gamelogic = tablelogic.get_gamelogic()
		gamelogic.run(table_data.gamelogic)
	end
end

function tabletimer.one_game_end_after_tostart(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.timer_id = -1
	local tablelogic = server:get_logicbyname("tablelogic")
	if table_data.state == HBSLTableState.TABLE_STATE_ONE_GAME_END_AFTER_OVER then
		local recent_buryinfo = tablelogic.get_online_bury()
    	if recent_buryinfo then
        	table_data.cur_landmineinfo = {}
			table_data.cur_landmineinfo.rid = recent_buryinfo.rid
			table_data.cur_landmineinfo.rolename = recent_buryinfo.rolename
			table_data.cur_landmineinfo.logo = recent_buryinfo.logo
			table_data.cur_landmineinfo.sex  = recent_buryinfo.sex
			table_data.cur_landmineinfo.total_money = recent_buryinfo.total_money
			table_data.cur_landmineinfo.total_hb_num = recent_buryinfo.total_hb_num
			table_data.cur_landmineinfo.landmine_num = recent_buryinfo.landmine_num
			table_data.cur_landmineinfo.already_qhb_num = 0
		    -- table_data.cur_landmineinfo.wait_qhbinfos = qhbtool.gen_all_hbinfos(table_data.cur_landmineinfo.total_money, 
		    -- 	table_data.cur_landmineinfo.total_hb_num, 1, math.floor(table_data.cur_landmineinfo.total_money * 3 /10))
			local get_leinum = skynet.call(".slotmgr", "lua", "get_leinum_nums", table_data.cur_landmineinfo.total_hb_num)
		    ----filelog.sys_error("---------tabletimer.get_leinum-----------", get_leinum)
		    local min_hb_money = base.get_random(18,27)
		    local real_min_money = math.floor(table_data.cur_landmineinfo.total_money / 100)
			if real_min_money > min_hb_money then
				min_hb_money = real_min_money
			end
			table_data.cur_landmineinfo.wait_qhbinfos = qhbtool.gen_all_hbinfos_bylandnum(table_data.cur_landmineinfo.total_money, 
					table_data.cur_landmineinfo.total_hb_num, min_hb_money, math.floor(table_data.cur_landmineinfo.total_money * 2 /table_data.cur_landmineinfo.total_hb_num), 
						table_data.cur_landmineinfo.landmine_num, get_leinum, get_leinum)
		    ---filelog.sys_error("----------cur_landmineinfo----------", table_data.cur_landmineinfo)
			table_data.state = HBSLTableState.TABLE_STATE_ONE_GAME_START
			local gamelogic = tablelogic.get_gamelogic()
			gamelogic.run(table_data.gamelogic)
		else
			table_data.state = HBSLTableState.TABLE_STATE_WAIT_PLAYER_BURY_LANDMINE
    	end
    end
end

function tabletimer.check_hbinfo_for_robot(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.check_hbinfo_timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.check_hbinfo_timer_id = -1
	local tablelogic = server:get_logicbyname("tablelogic")
	local tablerobothelper = server:get_logicbyname("tablerobothelper")
	if table_data.state == HBSLTableState.TABLE_STATE_WAIT_PLAYER_QHB then
		local state, will_qiang = skynet.pcall(skynet.call, ".slotmgr", "lua", "get_q_allhb")
		local de_state, limit_time = skynet.pcall(skynet.call, ".slotmgr", "lua", "get_delaytime")
		skynet.fork(
			function ()
				while (table_data.state == HBSLTableState.TABLE_STATE_WAIT_PLAYER_QHB) do
					tablerobothelper.check_hbinfo_for_robot(will_qiang, limit_time)
					local r_sleep_time = base.get_random(3,15)
					skynet.sleep(r_sleep_time)
				end
			end)
	end
end

function tabletimer.check_mlplayerinfo(timerid, noticemsg)
	local table_data = server.table_data
	if timerid ~= table_data.check_mlinfo_timer_id then return end
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.check_mlinfo_timer_id = -1
	local tablelogic = server:get_logicbyname("tablelogic")
	local tablerobothelper = server:get_logicbyname("tablerobothelper")
	tablerobothelper.check_mlplayerinfo()

end

function tabletimer.check_unlimit_qhb_outtime(timerid, noticemsg)
	--filelog.sys_error("--------tabletimer.check_unlimit_qhb_outtime-------", timerid, noticemsg)
	local table_data = server.table_data
	if table_data.conf.table_index ~= noticemsg.table_index then return end
	table_data.unlimit_check_hbover_timer = -1
	local tablelogic = server:get_logicbyname("tablelogic")
	---if table_data.all_unlimit_hb_timers[noticemsg.hb_id] == nil then return end
	local gamelogic = tablelogic.get_gamelogic()
	gamelogic.unlimit_qhb_over_check(table_data.gamelogic)
	local timentc = {
		table_index = table_data.conf.table_index,
	}
	table_data.unlimit_check_hbover_timer = timer.settimer(1 * 100, "check_unlimit_qhb_outtime", timentc)
	---
	local tablerobothelper = server:get_logicbyname("tablerobothelper")
    tablerobothelper.check_robot_numer()
    tablerobothelper.check_mlplayerinfo()
end

return tabletimer